﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ProjectVLIB_LAB04.KetNoiDB;
using System.Data.SqlClient;
using ProjectVLIB_LAB04.VOL;

namespace ProjectVLIB_LAB04.BLL
{
    class DMKhachBL
    {
        private DataAccess dataAccess;

        public DMKhachBL()
        {
            dataAccess = new DataAccess();
        }

        /// <summary>
        /// 1. Hiển thị danh sách khach
        /// </summary>
        /// <returns></returns>
        public DataTable GetData()
        {
            // cau query select
            string sql = "SELECT MaKhach, HoTen, DiaChi, DienThoai, Email FROM tbl_DMKhach ORDER BY MaKhach DESC ";

            DataTable dt = new DataTable();
            // do toan bo du lieu vao datatable
            dt = dataAccess.GetData(sql);
            return dt;
        }

        /// <summary>
        /// 2. Thêm khach hang
        /// </summary>
        /// <param name="obj">DMKhach</param>
        /// <returns>true</returns>
        public bool InsertData(DMKhach obj)
        {
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@HoTen", SqlDbType.NVarChar) { Value = obj.HoTen };
            param[1] = new SqlParameter("@DiaChi", SqlDbType.NVarChar) { Value = obj.DiaChi };
            param[2] = new SqlParameter("@DienThoai", SqlDbType.NVarChar) { Value = obj.DienThoai };
            param[3] = new SqlParameter("@Email", SqlDbType.NVarChar) { Value = obj.Email };

            // cau query select
            string sql = "INSERT INTO tbl_DMKhach  (HoTen, DiaChi, DienThoai, Email) VALUES (@HoTen, @DiaChi, @DienThoai,@Email)";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 3. Xóa khach hang
        /// </summary>
        /// <param name="MaKhach">MaKhach INT</param>
        /// <returns>true</returns>
        public bool DeleteData(int MaKhach)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaKhach", SqlDbType.Int) { Value = MaKhach };
            // cau query select
            string sql = "DELETE FROM tbl_DMKhach WHERE MaKhach = @MaKhach";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 4. sửa khach hang
        /// </summary>
        /// <param name="obj">DMKhach</param>
        /// <returns>true</returns>
        public bool UpdateData(DMKhach obj)
        {
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@HoTen", SqlDbType.NVarChar) { Value = obj.HoTen };
            param[1] = new SqlParameter("@DiaChi", SqlDbType.NVarChar) { Value = obj.DiaChi };
            param[2] = new SqlParameter("@DienThoai", SqlDbType.NVarChar) { Value = obj.DienThoai };
            param[3] = new SqlParameter("@Email", SqlDbType.NVarChar) { Value = obj.Email };
            param[4] = new SqlParameter("@MaKhach", SqlDbType.Int) { Value = obj.MaKhach };

            // cau query select
            string sql = "UPDATE tbl_DMKhach SET HoTen = @HoTen, DiaChi = @DiaChi, DienThoai = @DienThoai, Email = @Email  " +
                           " WHERE MaKhach = @MaKhach";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 5. Tìm theo ma khach hang
        /// </summary>
        /// <param name="MaKhach"></param>
        /// <returns></returns>
        public DataTable GetDataById(int MaKhach)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaKhach", SqlDbType.Int) { Value = MaKhach };
            // cau query select
            string sql = "SELECT * FROM tbl_DMKhach WHERE MaKhach = @MaKhach";
            DataTable dt = dataAccess.GetData(sql, param);

            return dt;
        }

        // 6. Login
        // 7. Logout
    }
}
